//
//  ConnectModel.m
//  UI网络block
//
//  Created by dllo on 15/5/15.
//  Copyright (c) 2015年 蓝鸥. All rights reserved.
//

#import "ConnectModel.h"

@implementation ConnectModel

- (void)dealloc
{
    [_receiveData release];
    [super dealloc];
}

- (void)createConnect:(NSString *)str
{
    NSURL * url = [NSURL URLWithString:str];
    
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    
    [request setHTTPMethod:@"GET"];
    
    [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.receiveData = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receiveData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.finishBlock(_receiveData);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
}

+ (void)connectWithURL:(NSString *)url block:(void(^)(NSData * data))block
{
    ConnectModel * model = [[ConnectModel alloc] init];
    model.finishBlock = block;
    [model createConnect:url];
    [model release];
}

@end
